package tgc.edu.szxt.demo.web.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.szxt.demo.custom.AjaxResult;
import tgc.edu.szxt.demo.custom.BaseController;
import tgc.edu.szxt.demo.custom.TablePageable;
import tgc.edu.szxt.demo.entity.Student;
import tgc.edu.szxt.demo.entity.SysRole;
import tgc.edu.szxt.demo.entity.SysUser;
import tgc.edu.szxt.demo.entity.TbClass;
import tgc.edu.szxt.demo.entity.Teacher;
import tgc.edu.szxt.demo.service.CourseService;
import tgc.edu.szxt.demo.service.SysRoleService;
import tgc.edu.szxt.demo.service.SysUserService;
import tgc.edu.szxt.demo.service.TeacherService;
import tgc.edu.szxt.demo.web.form.Student1From;
import tgc.edu.szxt.demo.web.form.StudentForm;
import tgc.edu.szxt.demo.web.form.TeacherForm;

@Controller
@RequestMapping("/teacher")
public class TeacherCountroller extends BaseController<Teacher, Integer, TeacherForm>{
	@Autowired
	SysUserService sysUserService;
	@Autowired
	TeacherService teacherService;
	@Autowired
	SysRoleService sysRoleService;
	@Autowired
	CourseService courseService;

	@SuppressWarnings("serial")
	public Specification<Teacher> buildSpec(TeacherForm form){
		Specification<Teacher> spec=new Specification<Teacher>() {

			@Override
			public Predicate toPredicate(Root<Teacher> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				String getUsername = form.getUsername();
				if(StringUtils.hasText( getUsername)) {
					rules.add(cb.like(root.get("username"), "%"+getUsername+"%"));
				}
				String name = form.getName();
				if(StringUtils.hasText(name)) {
					rules.add(cb.like(root.get("name"), "%"+name+"%"));
				}
				String search = form.getSearch();
				if(StringUtils.hasText(search)) {
					HashSet<Predicate> rules2=new HashSet<>();
					rules2.add(cb.like(root.get("name"), "%"+search+"%"));
					rules2.add(cb.like(root.get("username"), "%"+search+"%"));
					rules.add(cb.or(rules2.toArray(new Predicate[rules2.size()])));
				}
				
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
	
	
	@RequestMapping("/laoshi")
	public void laoshi() {
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public Object save(TeacherForm form) {
		Teacher tea = new Teacher();
		Integer id = form.getId();
		if(id!=null) {
			tea = teacherService.findById(id);
			BeanUtils.copyProperties(form,tea);
			try {
				teacherService.save(tea);
				return  new AjaxResult("修改成功！");
			} catch (Exception e) {
				return  new AjaxResult(false,"修改失败！");
			}
		}else {      
			BeanUtils.copyProperties(form,tea);
			List<SysRole> role = new ArrayList<>();
			role.add(sysRoleService.findById(2));
			tea.setRoles(role);
			BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); 
			String password = encoder.encode("1111");
			tea.setPassword(password);
			try {
				teacherService.save(tea);
				return  new AjaxResult("保存成功！");
			} catch (Exception e) {
				return  new AjaxResult(false,"保存失败！");
			}
		}
	}
	
	
	@RequestMapping(value = "/reset")
	@ResponseBody
	public Object reset(TeacherForm form) {
		Teacher tea = new Teacher();
		Integer id = form.getId();
		tea=teacherService.findById(id);
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); 
		String password = encoder.encode("1111");
		tea.setPassword(password);
		teacherService.save(tea);
		return new AjaxResult("密码重置成功");
	}
	
}
